package org.one.stone.soup.grfxML;

import java.util.Vector;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.mjdb.data.field.NullField;
import org.one.stone.soup.xml.XmlElement;
 /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataSettings extends NullField implements ITag {
	public static String DEFAULT_ID = "_Settings";
		DataState cache = new DataState(false);
		DataState antialias = new DataState(false);
		DataString filter = new DataString();
		DataSize fps = new DataSize(10);
		DataPoint frameSize = new DataPoint();
		DataColor backgroundColor = new DataColor(0, 0, 0);
public DataSettings() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.FILTER))
		{
			filter = new DataString();
			filter.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.CACHE))
		{
			cache= new DataState(false);
	  		cache.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.ANTIALIAS))
		{
			antialias= new DataState(false);
	  		antialias.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.FPS))
		{
			fps = new DataSize(10);
	  		fps.buildFromgrfxML(element);
		}
		else if(eleName.equals(Token.FRAME_SIZE))
		{
			frameSize = new DataPoint(TagModifier.POINT_TYPE_BOUNDS);
			frameSize.buildFromgrfxML(element,TagModifier.POINT_TYPE_BOUNDS);
		}
		else if(eleName.equals(Token.BACKGROUND_COLOR))
		{
			backgroundColor = new DataColor(0,0,0);
			backgroundColor.buildFromgrfxML(element);
		}
		else if( !NameHelper.isSystemName(element.getName()) )
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.SETTINGS,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.FILTER))
		{
			filter.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.CACHE))
		{
			cache.copyFromgrfxML(element);
		}
		if(eleName.equals(Token.ANTIALIAS))
		{
			antialias.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.FPS))
		{
	  		fps.copyFromgrfxML(element);
		}
		else if(eleName.equals(Token.FRAME_SIZE))
		{
			frameSize.copyFromgrfxML(element,TagModifier.POINT_TYPE_BOUNDS);
		}
		else if(eleName.equals(Token.BACKGROUND_COLOR))
		{
			backgroundColor.copyFromgrfxML(element);
		}
		else
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.SETTINGS,""+element.getStartLineNo()} );
		}
	}
}
public DataColor getBackgroundColor() {
	return backgroundColor;
}
public DataSettings getClone() {


	DataSettings newSettings = new DataSettings();




	Vector elements = getClonedElements();
	for(int loop=0;loop<elements.size();loop+=2)
	{
		Field oldObj = (Field)elements.elementAt(loop);
		Field newObj = (Field)elements.elementAt(loop+1);
		newSettings.replace(oldObj,newObj);
	}


	return newSettings;
}
public DataSettings getClone(String path) {


	DataSettings newSettings = new DataSettings();

	GrfxMLStoreProcessor.setCloneName( path,this,newSettings );

	Vector elements = getClonedElements(path);
	for(int loop=0;loop<elements.size();loop+=2)
	{
		Field oldObj = (Field)elements.elementAt(loop);
		Field newObj = (Field)elements.elementAt(loop+1);
		newSettings.replace(oldObj,newObj);
	}


	return newSettings;
}
public DataString getFilter() {
	return filter;
}
public DataSize getFps() {
	return fps;
}
public DataPoint getFrameSize() {
	return frameSize;
}
public void register(DataLinker store) {

	try{
		store.add(filter,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(fps,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(frameSize,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(backgroundColor,this);
	}
	catch(LockException le)
	{}//Dummy catch

}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj, boolean recursive) {

	if(filter==oldObj)
		filter=(DataString)newObj;

	if(fps==oldObj)
		fps=(DataSize)newObj;

	if(frameSize==oldObj)
		frameSize=(DataPoint)newObj;

	if(backgroundColor==oldObj)
		backgroundColor=(DataColor)newObj;
}
public void resetChanged() {
	super.resetChanged();
	filter.resetChanged();
	fps.resetChanged();
	frameSize.resetChanged();
	backgroundColor.resetChanged();
}
protected void setBackgroundColor(DataColor newBackgroundColor) {
	backgroundColor = newBackgroundColor;
}
protected void setFilter(DataString newFilter) {
	filter = newFilter;
}
protected void setFps(DataSize newFps) {
	fps = newFps;
}
protected void setFrameSize(DataPoint newFrameSize) {
	frameSize = newFrameSize;
}
public String togrfxML(String name) {
	String xml = "<"+name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml +=  ">";

	xml += filter.togrfxML("filter");
	xml += fps.togrfxML(new DataSize(10),"fps");
	xml += frameSize.togrfxML("frameSize");
	xml += backgroundColor.togrfxML("backgroundColor");
	xml += cache.togrfxML("cache");
	xml += antialias.togrfxML("antialias");
	xml += "</" + name + ">\n";
	return xml;
}
public String togrfxML(String name,TagModifier modifier) {
	return togrfxML(name);
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
		/**
		 * @return Returns the antialias.
		 */
		public DataState getAntialias() {
			return antialias;
		}
}
